﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Net;
using ZyGames.Framework.Common.Serialization;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 移动端根据好友ID来获得好友详细信息列表
	/// </summary>
	public class Action30072 : BaseAction
	{
		private string userIDStr;
		private string simpleUsersInfo;
		public Action30072(HttpGet actionGetter)
			: base((short)ActionType.DZGetFriendsInfo, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetString("UserIDStr", ref userIDStr))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			if(string.IsNullOrEmpty(userIDStr))
			{
				Tool.WriteErrorLine("300072没有获得玩家列表信息");
				return false;
			}

			var ids = ProtoBufUtils.Deserialize<List<int>>(Convert.FromBase64String(userIDStr));
			if(ids.Count <= 0)
			{
				Tool.WriteErrorLine("300072获得玩家列表反序列化没有得到一位玩家的id号");
				return false;
			}
			var simpleUsers = new List<SimpleUserData>();
			foreach(var id in ids)
			{
				var user = GameTable.Current.GetUser(id);
				if(user != null)
				{
					simpleUsers.Add(new SimpleUserData() { UserId = id, HeadIcon = user.HeadIcon, NickName = user.GetNickName() });
				}
			}
			simpleUsersInfo = Convert.ToBase64String(ProtoBufUtils.Serialize(simpleUsers));
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this.simpleUsersInfo);
		}
	}
}
